package com.mrobuy.jd.api;

import com.google.gson.Gson;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.find.ApplyProductDto;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.find.MyProductInfoDto;
import com.jd.open.api.sdk.request.supplier.VcItemProductAppliesFindRequest;
import com.jd.open.api.sdk.request.supplier.VcItemProductsFindRequest;
import com.jd.open.api.sdk.response.supplier.VcItemProductAppliesFindResponse;
import com.jd.open.api.sdk.response.supplier.VcItemProductsFindResponse;
import com.mrobuy.jdvc.entity.VCProduct;
import com.mrobuy.jdvc.entity.VCProductApply;
import com.mrobuy.jdvc.entity.VCProductInfo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 *  获取我的商品列表
 *
 *  jingdong.vc.item.products.find
 *
 *  <url>https://open.jd.com/home/home#/doc/api?apiCateId=86&apiId=1681&apiName=jingdong.vc.item.products.find</url>
 *
 *
 *
 */
public class ProductInfoFindClient extends JosClient<List<VCProductInfo>> {

    private static final int PAGE_SIZE = 50;

    private VcItemProductsFindRequest request=new VcItemProductsFindRequest();

    public ProductInfoFindClient(JdClient client) {
        super(client);
    }

    @Override
    public List<VCProductInfo> call() throws Exception {

        List<VCProductInfo> result = new ArrayList<>();

        request.setPageSize(PAGE_SIZE);
        int offset = 0;

        while ( true ) {
            request.setOffset(offset);

            LOG.debug("[REQ]" + request.getApiMethod() + ": " + request.getAppJsonParams());

            VcItemProductsFindResponse response= client.execute(request);
            if ( response==null ) {
                throw new RuntimeException("API调用异常: response is null!");
            }

            if ( response.getJosResultDto()==null ) {
                throw new RuntimeException("API调用异常: response.getJosResultDto() is null!");
            }

            if ( !response.getJosResultDto().getIsSuccess() ) {
                throw new RuntimeException(response.getJosResultDto().getReturnMessage());
            }

            LOG.debug("总记录数目: " + response.getJosResultDto().getCount());

            List<MyProductInfoDto> productInfoDtos = response.getJosResultDto().getResult();
            for (MyProductInfoDto productInfoDto : productInfoDtos) {
                VCProductInfo productInfo = new VCProductInfo();
                productInfo.setWareId(productInfoDto.getWareId());
                productInfo.setWareName(productInfoDto.getWareName());
                productInfo.setSaleState(productInfoDto.getSaleState()); // 2: 可上柜; 1: 上柜; 0: 下柜
                productInfo.setIsPrimary(productInfoDto.getIsPrimary());
                productInfo.setIsGaea(productInfoDto.getIsGaea());
                productInfo.setModifyTime(productInfoDto.getModifyTime());
                productInfo.setUpdateTime(Calendar.getInstance().getTime());
                result.add(productInfo);
            }

            if ( response.getJosResultDto()==null || response.getJosResultDto().getResult()==null
                    || response.getJosResultDto().getResult().size()<PAGE_SIZE || response.getJosResultDto().getResult().size()==0  ) {
                break;
            }

            offset += productInfoDtos.size();
        }

        return result;
    }

    public ProductInfoFindClient setWareId(String ware_id) {
        request.setWareId(ware_id);
        return this;
    }

    public ProductInfoFindClient setCategoryId(Integer cateId) {
        request.setCategoryId(cateId);
        return this;
    }

    public ProductInfoFindClient setBrandId(Integer brandId) {
       request.setBrandId(brandId);
        return this;
    }
}
